package cn.tedu.imop.emqx.service;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MqttProducer {

    private static Logger log= LoggerFactory.getLogger(MqttProducer.class);
    @Autowired
    private MqttClient mqttClient;

    public Boolean send(String topic,String message)  {
        Boolean success = false;
        try {
            mqttClient.publish(topic,message.getBytes(),0,true);
            success = true;
        } catch (MqttException e) {
            //接入告警系统 1秒钟超过3次失败 告警
            log.error("给EMQX发送消息失败,topic:{},消息:{}",topic,message,e);
        }
        return success;
    }
}
